package org.opicasso.cheese.ui.pages;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.model.CompoundPropertyModel;
import org.opicasso.cheese.domain.Cheese;
import org.opicasso.cheese.ui.components.PriceLabel;
import org.opicasso.cheese.ui.components.ShoppingCartPanel;

/**
 * Homepage
 */
public class HomePage extends AbstractCheesePage {

	private static final long serialVersionUID = -5092298224909076439L;

	public HomePage() {
		// cheese list
		final PageableListView cheeses = new PageableListView("cheeses",
				getCheeses(), 5) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(final ListItem item) {
				item.setModel(new CompoundPropertyModel(item.getModelObject()));
				final Cheese cheese = (Cheese) item.getModelObject();
//				item.add(new Label("name"));
				item.add(new Label("description"));
				item.add(new PriceLabel("price", cheese.getPrice()));
				item.add(new Link("add", item.getModel()) {

					private static final long serialVersionUID = 5576459957261136311L;

					@Override
					public void onClick() {
						getCart().getCheeses().add((Cheese) getModelObject());
					}
				});
				// cheese details link
				final Link detailsLink = new Link("details"){
					private static final long serialVersionUID = 1200558711528203672L;

					@Override
					public void onClick() {
						setResponsePage(new CheesePage((Cheese) item.getModelObject()));
					}};
				item.add(detailsLink);
				detailsLink.add(new Label("name"));
			}
		};
		add(cheeses);
		add(new PagingNavigator("navigator", cheeses));

		// cart
		add(new ShoppingCartPanel("cart", getCart()));

		// checkout
		add(new Link("checkout") {

			private static final long serialVersionUID = -30776011542801575L;

			@Override
			public void onClick() {
				setResponsePage(new CheckoutPage());
			}

			@Override
			public boolean isVisible() {
				return !getCart().getCheeses().isEmpty();
			}
		});

	}
}
